import React from 'react';
import { Select,Button,Input ,Table,DatePicker } from 'antd';
import { Link } from 'react-router';
import './operationLog.less';
import config from '../../config/config';
import $ from 'jquery';
const token = sessionStorage.getItem('token');
const Option = Select.Option;
const Search = Input.Search;
const allRouter=config.router;
const {  RangePicker, } = DatePicker;
class OperationLog extends React.Component {
    constructor(props){
        super(props);
        this.state={
            data:[],
            timevalue:'',
            txtvalue:'',
            selectRows:[],
            pagination: {
                pageIndex: 1,
                pageSize: 10,
                total: 0,
                onChange: (page, pageSize) => {
                    console.log('current page: ', page);
                    const pagination = { ...this.state.pagination };
                    pagination.pageIndex = page;
                    this.setState({
                        pagination
                    });
                    console.log(this.state.pagination);
                    this.getList(page);
                }
            },
            loading: false,
            selectvalue:'1',
        }
    }
    componentWillMount() {

    }
    componentDidMount() {
        this.getList(1);
    }
//获取日志列表
    getList(num) {
        let url=allRouter.patientOrigin+'/operationLog/find';
        console.log(this.state.timevalue);
        $.ajax({
            url:url,
            type: "post",
            headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
            data:{
                pagesize: this.state.pagination.pageSize,
                pageindex:num,
                starttime:this.state.timevalue[0],
                endtime:this.state.timevalue[1],
                results:this.state.selectvalue,
                text:this.state.txtvalue,
            },
            async: true,
            success:(d)=> {
                console.log(d);
                const pagination = { ...this.state.pagination };
                // Read total count from server
                // pagination.total = data.totalCount;
                pagination.total = d.counts;
                this.setState({
                    loading: false,
                    data:d.data,
                    pagination,
                });
                console.log(this.state)
            }
        });
    }
    downList() {
        console.log(this.state.selectRows);
        let url=allRouter.patientOrigin+'/operationLog/downloadExcel';
        //console.log(url);
        var para;
        if(this.state.selectRows.length>0){
                para={
                    pagesize: '',
                    pageindex:'',
                    results:this.state.selectvalue,
                    text:this.state.txtvalue,
                    arr:JSON.stringify(this.state.selectRows),
                    starttime:this.state.timevalue[0],
                    endtime:this.state.timevalue[1],
                }
        }else{
            para={
                pagesize: '',
                pageindex:'',
                results:this.state.selectvalue,
                text:this.state.txtvalue,
                starttime:this.state.timevalue[0],
                endtime:this.state.timevalue[1],
            };
        }
        $.ajax({
            url:url,
            type: "post",
            headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
            data:para,
            async: true,
            success: (d)=> {
                console.log(d);
                self.location=allRouter.patientOrigin+'/operationLog/download?name='+d.downloadExcel;
            }
        });
    }
    handleChange(value) {
        console.log(`selected ${value}`);
        this.setState({
            selectvalue:value
        })

    }
    onChange(date, dateString) {
        console.log(date, dateString);
        let url=allRouter.patientOrigin+'/operationLog/find';
        this.setState({
            timevalue:dateString
        });
        $.ajax({
            url:url,
            type: "post",
            headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
            data:{
                pagesize: this.state.pagination.pageSize,
                pageindex:1,
                results:this.state.selectvalue,
                text:this.state.txtvalue,
                starttime:dateString[0],
                endtime:dateString[1]
            },
            async: true,
            success:(d)=> {
                console.log(d);
                const pagination = { ...this.state.pagination };
                // Read total count from server
                // pagination.total = data.totalCount;
                pagination.total = d.counts;
                this.setState({
                    loading: false,
                    data:d.data,
                    pagination,
                });
                console.log(this.state)
            }
        });
    }
    onserch(value){
        console.log(value);
        this.setState({
            txtvalue:value
        });
        let url=allRouter.patientOrigin+'/operationLog/find';
        $.ajax({
            url:url,
            type: "post",
            headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
            data:{
                pagesize: this.state.pagination.pageSize,
                pageindex:1,
                text:value,
                results:this.state.selectvalue,
                starttime:this.state.timevalue[0],
                endtime:this.state.timevalue[1]
            },
            async: true,
            success:(d)=> {
                console.log(d);
                const pagination = { ...this.state.pagination };
                // Read total count from server
                // pagination.total = data.totalCount;
                pagination.total = d.counts;
                this.setState({
                    loading: false,
                    data:d.data,
                    pagination,
                });
                console.log(this.state)
            }
        });
    }
       render(){
        const columns = [{
            title: '操作角色',
            dataIndex: 'name',
            render: text => <a href="#">{text}</a>,
        }, {
            title: '操作结果',
            dataIndex: 'Operationcontent',
        }, {
            title: '操作IP',
            dataIndex: 'ip',
        }, {
            title: '操作时间',
            dataIndex: 'time',
        }];
        const rowSelection = {
            onChange: (selectedRowKeys, selectedRows) => {
                console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
                var rows=[];
                for(var i=0;i<selectedRows.length;i++){
                    rows.push(selectedRows[i]._id);
                }
                this.setState({
                    selectRows:rows
                });
            },
            getCheckboxProps: record => ({
                disabled: record.name === 'Disabled User', // Column configuration not to be checked
                name: record.name,
            }),
        };
        return (
            <div className="container">
                <Button>操作日志</Button>
                <div className="czrz">
                    <span>选择时间：</span>
                    <RangePicker onChange={this.onChange.bind(this)} />
                    <Select defaultValue="1" style={{ width: 120,marginLeft:20 }} onChange={this.handleChange.bind(this)}>
                        <Option value="1">操作角色</Option>
                        <Option value="2">操作结果</Option>
                    </Select>
                    <Search
                        placeholder="input search text"
                        onSearch={this.onserch.bind(this)}
                        enterButton
                    />
                    <Button className="daochu" type="primary" onClick={this.downList.bind(this)}>导出Excel</Button>
                </div>
                <div className="czrz1"><Table rowSelection={rowSelection} columns={columns} dataSource={this.state.data} pagination={this.state.pagination} rowKey="_id"/></div>

            </div>
        );
    }
}
export default OperationLog;